<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
    <style>
        #snapshots {
            position: absolute;
            left: 10px;
            top: 60px;
            margin-top: 20px;
            overflow-y: scroll;
            height: calc(100% - 120px);
            margin-bottom: 10px;
            width: 230px;
            pointer-events: all;
            opacity: 0.8;
        }
    </style>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div id="snapshots"></div>
<div class="slideout-sidebar">
    <h1>Capturing PNG Screenshots</h1>
    <p>Using viewer.getSnapshot()</p>
    <h3>Components used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html"
               target="_other">XKTLoaderPlugin</a>

        </li>
    </ul>
    <h3>Resources</h3>
    <ul>
        <li>
            <a href="https://github.com/openBIMstandards/DataSetSchependomlaan"
               target="_other">Model source</a>
        </li>
    </ul>
</div>
</body>

<script type="module">

    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {Viewer, XKTLoaderPlugin} from "../../dist/xeokit-sdk.min.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer, arrange the camera
    //------------------------------------------------------------------------------------------------------------------

    const viewer = new Viewer({
        canvasId: "myCanvas",
        transparent: true, // Default
        preserveDrawingBuffer: true // Default - needed true for canvas screenshots
    });

    viewer.camera.eye = [-3.933, 2.855, 27.018];
    viewer.camera.look = [4.400, 3.724, 8.899];
    viewer.camera.up = [-0.018, 0.999, 0.039];

    //----------------------------------------------------------------------------------------------------------------------
    // Load a model and fit it to view
    //----------------------------------------------------------------------------------------------------------------------

    const xktLoader = new XKTLoaderPlugin(viewer);

    const sceneModel = xktLoader.load({
        id: "myModel",
        src: "../../assets/models/xkt/v10/glTF-Embedded/Duplex_A_20110505.glTFEmbedded.xkt",
        edges: true
    });

    var canvas = viewer.scene.canvas;

    //------------------------------------------------------------------------------------------------------------------
    // When model loaded, start capturing PNG screenshots
    //------------------------------------------------------------------------------------------------------------------

    sceneModel.on("loaded", function () {

        const screenshotsDiv = document.getElementById("snapshots");
        var numScreenshots = 0;

        setInterval(() => {

            if (numScreenshots++ > 5) {
                return;
            }

            viewer.camera.orbitYaw(10);

            const canvas = viewer.scene.canvas;
            const canvasElement = canvas.canvas;
            const imageData = canvasElement.toDataURL();
            const img = document.createElement("img");
            const aspect = canvasElement.height / canvasElement.width;
            const width = 200;
            const height = Math.floor(width * aspect);

            img.src = imageData;
            img.style.border = "1px solid #000000";
            img.style.width = width + "px";
            img.style.height = height + "px";
            img.style.background = "lightblue";
            img.width = width + "px";
            img.height = height + "px";

            screenshotsDiv.appendChild(img);

        }, 1000);
    });

</script>
</html>
